<?php
namespace app\index\controller;
use app\admin\model\Ctbp;
use app\admin\model\Fjxx;
use app\admin\model\Gbt;
use app\admin\model\Hhl;
use app\admin\model\Xmfl;
use app\admin\model\Xmxx;
use think\Controller;
use app\admin\model\Auth as AuthModel;
use app\admin\model\Fjxx as FjxxModel;
use app\admin\model\Gbt as GbtModel;
use app\admin\model\Cbfs as CbfsModel;
use think\Db;

class Index extends Controller
{
    public function index()
    {

    }

    public function getmenuitems(){
        $mid = $this->request->param('mid');
        $plist = AuthModel::where('pid',0)->where('menu_id',$mid)->select();
        foreach($plist as $k=>$v){
            $plist[$k]['children'] = AuthModel::where('pid',$v['id'])->select();
        }
        return json_encode($plist);
    }

    // 获取房间信息by房间id
    public function getFjxxByFjid(){
        $id = $this->request->param('id');
        if($fjxx = FjxxModel::get($id)){
            $res = array(
                'statusCode' => 200,
                'message' => '操作成功',
                'data' => array('cs_price'=>$fjxx->price)
            );
        }else{
            $res = array(
                'statusCode' => 300,
                'message' => '操作失败',
            );
        }
        return json_encode($res);
    }

    // 获取告别厅信息by Title
    public function getGbtxxByTitle(){
        $title = $this->request->param('title');
        if($gbt = GbtModel::get(['title'=>$title])){
            $res = array(
                'statusCode' => 200,
                'message' => '操作成功',
                'data' => array('price'=>$gbt->price)
            );
        }else{
            $res = array(
                'statusCode' => 300,
                'message' => '操作失败',
            );
        }
        return json_encode($res);
    }

    // 获取出殡信息by Title
    public function getCbxxByTitle(){
        $title = $this->request->param('title');
        if($info = CbfsModel::get(['title'=>$title])){
            $res = array(
                'statusCode' => 200,
                'message' => '操作成功',
                'data' => array('price'=>$info->price)
            );
        }else{
            $res = array(
                'statusCode' => 300,
                'message' => '操作失败',
            );
        }
        return json_encode($res);
    }

    // 获取预定炉次信息by id
    public function getYdlcxxById(){
        $id = $this->request->param('id');
        $cbrq = $this->request->param('cbrq');
        if($list = Db::name('hhllc')
            ->where('hhlid',$id)
            //->where('use_date','<>',date('Y-m-d',strtotime($cbrq)))
            ->select())
        {
            $cblist = CbfsModel::all();
            foreach($list as $k=>$v){
                if($hhl_use = Db::name('register_cb')->where('ydlcid',$v['id'])->where('cbrq',strtotime($cbrq))->find()){
                    $list[$k]['status'] = 1;
                }else{
                    $list[$k]['status'] = 0;
                }
            }
            foreach($cblist as $k=>$v){
                if($cbfs_use = Db::name('register_cb')->where('cbfsid',$v['id'])->where('cbrq',strtotime($cbrq))->find()){
                    $cblist[$k]['status'] = 1;
                }else{
                    $cblist[$k]['status'] = 0;
                }
            }
            $res = array(
                'statusCode' => 200,
                'message' => '操作成功',
                'data' => array('list'=>$list),
                'cbdata' => array('list'=>$cblist),
            );
        }else{
            $res = array(
                'statusCode' => 300,
                'message' => '操作失败'.date('Y-m-d',strtotime($cbrq)),
            );
        }
        return json_encode($res);
    }

    // 获取预定炉次信息by title
    public function getYdlcxxByTitle(){
        $title = $this->request->param('title');   //regid
        $cbrq = $this->request->param('cbrq');
        $hhl = Hhl::get(['title'=>$title]);
        if($list = Db::name('hhllc')
            ->where('hhlid',$hhl->id)
            ->select())
        {
            $cblist = CbfsModel::all();
            foreach($list as $k=>$v){
                if($hhl_use = Db::name('register_cb')->where('ydlcid',$v['id'])->where('cbrq',$cbrq)->find()){
                    $list[$k]['status'] = 1;
                }else{
                    $list[$k]['status'] = 0;
                }
            }
            foreach($cblist as $k=>$v){
                if($cbfs_use = Db::name('register_cb')->where('cbfsid',$v['id'])->where('cbrq',$cbrq)->find()){
                    $cblist[$k]['status'] = 1;
                }else{
                    $cblist[$k]['status'] = 0;
                }
            }
            $res = array(
                'statusCode' => 200,
                'message' => '操作成功',
                'data' => array('list'=>$list),
                'cbdata' => array('list'=>$cblist),
            );
        }else{
            $res = array(
                'statusCode' => 300,
                'message' => '操作失败',
            );
        }
        return json_encode($res);
    }

    // 订单单号处理
    public function formatDddh($id,$djbz){
        $bm = '';
        switch ($djbz){
            case 0:
                $bm = 'QTDJ';
                break;
            case 1:
                $bm = 'CJDJ';
                break;
            case 2:
                $bm = 'SJSDJ';
                break;
            case 3:
                $bm = 'WMDJ';
                break;
        }
        $dhjg = '';         // 单号间隔 0
        if($id<10){
            $dhjg = '0000';
        }else if($id<100){
            $dhjg = '000';
        }else if($id<1000){
            $dhjg = '00';
        }else if($id<10000){
            $dhjg = '0';
        }else{
            $dhjg = '';
        }
        if($bm and $dhjg and $id){
            return $bm.date('Ymd').'-'.$dhjg.$id;
        }else{
            return false;
        }
    }


    // 获取项目信息 by 关键字
    public function getXmxx(){
        $search = $this->request->param('search');
        $xmxx = \app\admin\model\Xmxx::where('xmbm|xmmc','like','%'.$search.'%')->order('xmbm','asc')->select();
        return json_encode($xmxx);
    }
    // 获取项目信息 by id
    public function getXmxxById(){
        $search = $this->request->param('search');
        $xmxx = \app\admin\model\Xmxx::where('id',$search)->find();
        return json_encode($xmxx);
    }

    // 获取成套殡品信息
    public function getCtbpById(){
        $search = $this->request->param('search');
        $ctbp = Ctbp::get($search);
        $datas = $ctbp->datas;
        $list = [];
        $totalCounts = 0;
        $totalPrice = 0;
        $xmdatas = json_decode($datas,true);
        if($xmdatas){
            foreach($xmdatas as $xk=>$xv){
                $xmlist = Xmxx::where('id',$xv['xm_id'])->find();
                $xmfl = Xmfl::where('id',$xmlist['flid'])->where('status',1)->find();
                if($xmlist){
                    $xmlist['title'] = $xmfl['title'];
                    $xmlist['nums'] = $xv['nums'];
                    $xmlist['totalje'] = $xv['nums']*$xmlist['sj'];
                    $list[] = $xmlist;
                    $totalCounts += 1;
                    $totalPrice += $xmlist['totalje'];
                }
            }
        }
        echo json_encode($list);
    }





}
